/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Course;
import com.hboxs.asl.entity.Record;
import com.hboxs.asl.qcloudLVB.entity.Live;
import com.hboxs.asl.qcloudLVB.service.LiveService;
import com.hboxs.asl.service.CourseService;
import com.hboxs.asl.service.RecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;

import javax.annotation.Resource;

/**
 * Controller - 删除记录
 */
@Controller("adminRecordController")
@RequestMapping("/asl-admin/record")
public class RecordController extends BaseController {

    @Resource(name = "recordServiceImpl")
    private RecordService recordService;
    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "liveServiceImpl")
    private LiveService liveService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        return "/admin/record/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Record record, RedirectAttributes redirectAttributes) {
        if (!isValid(record)) {
            return ERROR_VIEW;
        }
        recordService.save(record);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("record", recordService.find(id));
        model.addAttribute("statues", Record.Status.values());
        return "/admin/record/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Record record, RedirectAttributes redirectAttributes) {
        if (!isValid(record)) {
            return ERROR_VIEW;
        }
        if(record.getStatus().equals(Record.Status.passed)) {
            if (record.getCourseId() != null) {
                Course course = courseService.find(record.getCourseId());
                Live live = course.getLive();
                if (live != null) {
                    liveService.delete(live.getId());
                } else {
                    courseService.delete(course.getId());
                }
            }
        }
        recordService.update(record, "courseId", "title", "username");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", recordService.findPage(pageable));
        return "/admin/record/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        recordService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
